package com.shuyihui.identity

import android.app.Application
import android.graphics.Bitmap
import android.util.Log
import androidx.lifecycle.viewModelScope
import com.owbtgad.common.base.BaseViewModel
import com.owbtgad.common.base.NetLiveData
import com.owbtgad.common.utils.InsertHelp
import com.shuyihui.rxhttp.EasyHttp
import com.shuyihui.rxhttp.callback.SimpleCallBack
import com.shuyihui.rxhttp.exception.ApiException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.MultipartBody
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.io.InputStream

/**
 * 创建日期 2021/6/8
 * 描述：
 */
class CertificationViewModel(application: Application, private val repo: IdentityRepo) : BaseViewModel(
    application
) {
    val netLiveData : NetLiveData<String> = NetLiveData()

    fun getAuthConfig(){
        viewModelScope.launch(Dispatchers.IO) {
            repo.getAuthConfig(netLiveData)
        }
    }
    fun upload(parts: List<MultipartBody.Part>){
        viewModelScope.launch(Dispatchers.IO) {
            repo.upload(parts, netLiveData)
        }
    }

    /*fun authCommonUploadAd(
        isBm: InputStream,
        type: String?
    ) {
        *//*val baos = ByteArrayOutputStream()
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos)
        val isBm: InputStream = ByteArrayInputStream(baos.toByteArray())*//*
        val head = "{\"deviceType\":\"BMH-AN10\",\"appVersion\":\"1.0.0\",\"netType\":\"WIFI\",\"channel\":\"1\",\"screen\":\"1080x2289\",\"appCode\":\"002\",\"deviceId\":\"00000000-0cf7-4663-0000-0000521edc7f\",\"userId\":\"138\",\"reqId\":\"2afdfd1d-065e-4124-b462-7f10d9208a6f\",\"token\":\"yCT42dHK+1eX4WYN45ui9OCrnnZsAvaAQhOs8rr26TesVla8h8I/Cf2hIXyPer1Qr7IJzG4PVKuSO7HkvUB5uw\\u003d\\u003d\",\"osVersion\":\"29\",\"wifiName\":\"unknown+id\",\"clientIp\":\"122.233.172.138\",\"osType\":\"android\",\"region\":\"MX\",\"timestamp\":" +
                "\"" +
                System.currentTimeMillis().toString() +
                "\"" +
                ",\"channelCode\":\"1\",\"appLanguage\":\"Chinese\"}"
        EasyHttp.post("/api/android/authCommon/upload/v1")
            .headers("systemData", head)
            .params("file", isBm, System.currentTimeMillis().toString() + ".jpg", null)
            .params("type", type)
            .baseUrl("http://tapi.sunton.mx")
            .execute(object : SimpleCallBack<String>() {
                override fun onError(e: ApiException?) {
                    Log.e(TAG, "onError: ", e)
                }

                override fun onSuccess(t: String?) {
                    Log.d(TAG, "onSuccess: $t")
                }

            })
    }*/

    fun authCommonUploadAd(
        bitmap: Bitmap,
        type: String?
    ) {

        val head = "{\"reqId\":\"1\",\"deviceId\":\"ffffffff-98ac-907a-0000-000000000064\",\"screen\":\"1080x1920\",\"appCode\":\"111\",\"userId\":\"154\",\"token\":\"MjuXWibfVdh9ApyIsoi1d1cwtJBLL+phqmzlws0svcVzPm655b8tarWkRwjZ3peBwuFH41zzLJeSE+PVaQoBuw\\u003d\\u003d\",\"region\":\"MX\",\"deviceType\":\"PCLM10\",\"timestamp\":\"1646796314128\",\"appLanguage\":\"English\",\"wifiName\":\"wifi\",\"channelCode\":\"2\",\"clientIp\":\"172.16.1.15\",\"appVersion\":\"1.2.1\",\"osType\":\"android\",\"osVersion\":\"25\",\"netType\":\"WIFI\",\"channel\":\"1\"}"
        val baos = ByteArrayOutputStream()
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos)
        val isBm: InputStream = ByteArrayInputStream(baos.toByteArray())
        EasyHttp.post("http://loan.syh.biz/api/android/authCommon/upload/v1")
//            .baseUrl("http://loan.syh.biz")
//            .headers("systemData", InsertHelp.getHttpHeads(mContext))
            .headers("systemData", head)
            .params("file", isBm, System.currentTimeMillis().toString() + ".jpg", null)
            .params("type", type)
//            .baseUrl("http://tapi.sunton.mx")
            .execute(object : SimpleCallBack<String>() {
                override fun onError(e: ApiException?) {
                    try {
                        isBm.close()
                    } catch (ex: IOException) {
                        ex.printStackTrace()
                    }
                }

                override fun onSuccess(t: String?) {
                    try {
                        isBm.close()
                    } catch (ex: IOException) {
                        ex.printStackTrace()
                    }
                }
                /*fun onError(e: ApiException?) {
                    try {
                        isBm.close()
                    } catch (ex: IOException) {
                        ex.printStackTrace()
                    }

                }*/

                /*fun onSuccess(string: String) {
                    try {
                        isBm.close()
                    } catch (ex: IOException) {
                        ex.printStackTrace()
                    }
                }*/
            })
    }
}